<?php


namespace App\Http\Business;

use Illuminate\Support\Str;
use Illuminate\Support\Facades\Request;
use Gregwar\Captcha\CaptchaBuilder;

class CaptchasBusiness extends BaseBusiness
{
    const CAPTCHA_EXPIRE_TIME = 5;          //验证码过期时间
    const CAPTCHA_KEY_VALUE = 'captcha-';   //缓存验证码的key

    const CAPTCHA_SHOW_KEY_VALUE = 'is_show_captcha_key_';   //是否显示验证码
    const CAPTCHA_SHOW_KEY_CACHE_TIME = 7200;        //显示验证码缓存时间(秒)-两小时
    const CAPTCHA_VERIFY_RES_CACHE_KEY = 'captcha_verify_res_';  //验证码验证结果
    const CAPTCHA_VERIFY_RES_CACHE_TIME = 5; //验证码验证结果缓存时间


    protected $clientIp;

    public function __construct()
    {
        $this->clientIp = Request::ip();
    }
    /**
     * 创建验证码
     * @param null $phone
     * @return array
     */
    public function build($phone = null)
    {
        $key = self::CAPTCHA_KEY_VALUE . Str::random(15);
        $captchaBuilder = new CaptchaBuilder();
        $captcha = $captchaBuilder->build();
        $expiredAt = now()->addMinutes(self::CAPTCHA_EXPIRE_TIME);
        \Cache::put($key, ['ip' => $this->clientIp, 'phone' => $phone, 'code' => $captcha->getPhrase()], $expiredAt);

        $result = [
            'captcha_key' => $key,
            'expired_at' => $expiredAt->toDateTimeString(),
            'captcha_image_content' => $captcha->inline()
        ];

        return $result;
    }

    /**
     * 是否显示验证码
     * @param string $type
     * @return bool
     */
    public function show($type = 'admin')
    {
        $ip = $this->clientIp;
        $key = self::CAPTCHA_SHOW_KEY_VALUE . $ip .$type;

        $isShow = \Cache::get($key);
        if ($isShow){
            return true;
        }
        return false;
    }

    /**
     * 设置显示验证码缓存
     * @param string $type
     * @return bool
     */
    public function setCaptcha($type = 'admin')
    {
        $ip = $this->clientIp;
        $key = self::CAPTCHA_SHOW_KEY_VALUE . $ip . $type;

        \Cache::put($key, 1, self::CAPTCHA_SHOW_KEY_CACHE_TIME);

        return true;
    }

    /**
     * 删除显示验证码缓存
     * @param string $type
     * @return bool
     */
    public function delCaptcha($type = 'admin')
    {
        $ip = $this->clientIp;
        $key = self::CAPTCHA_SHOW_KEY_VALUE . $ip . $type;

        \Cache::forget($key);

        return true;
    }


    /**
     * @param $captchaKey
     * @param $value
     * @param int $type type=1时候用于提示验证码是否正确
     * @return bool
     */
    public function verifyCaptcha($captchaKey, $value, $type = 1)
    {
        if ($type == 1){
            $captchaValue = \Cache::get($captchaKey);
        }else{
            $captchaValue = \Cache::pull($captchaKey);
        }

        if ($captchaValue && mb_strtolower($value) == mb_strtolower($captchaValue['code'])){
            //验证后缓存结果
            if ($type == 2){
                $resKey = self::CAPTCHA_VERIFY_RES_CACHE_KEY . $captchaKey;
                \Cache::put($resKey, 1, self::CAPTCHA_VERIFY_RES_CACHE_TIME);
            }
            return true;
        }

        return false;
    }

    /**
     * 是否通过验证码
     * @param $captchaKey
     * @return mixed
     */
    public function getVerifyRes($captchaKey)
    {
        $resKey = self::CAPTCHA_VERIFY_RES_CACHE_KEY . $captchaKey;
        $resValue = \Cache::pull($resKey);

        return $resValue;
    }

}
